package com.ftg.examsys.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ftg.examsys.service.RedisUtilService;
import com.ftg.examsys.service.UserService;
import com.ftg.examsys.util.PageEx;
import com.ftg.examsys.util.ResBuilder;
import com.ftg.examsys.util.ResUtil;
import com.ftg.examsys.vo.UserDetailVo;
import com.ftg.examsys.vo.UserInfoVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 用户 控制器
 * <p>
 * 表示这可以让页面客户端访问 了，
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/9/9 上午10:22
 */
@Api(tags = "用户管理模块")
@RestController
public class UserController {

    @Resource
    UserService userService;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * GetMapping 对应的 select语句调用
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "帐号", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
    })
    @ApiOperation("用来完成用户登陆功能的方法")
    @PostMapping("/login")
    public ResUtil<UserInfoVo> login(@RequestParam("username") String username, @RequestParam("password") String password) {
        UserInfoVo login = userService.login(username, password);
        return login != null ? ResBuilder.ok(login) : ResBuilder.failure();
    }


    /**
     * 0密码加密
     *
     * @param username 帐号
     * @param realname 真实姓名
     * @param gender   性别
     * @param number   编号
     * @param roleIds  角色编号
     * @return
     */
    @ApiOperation("用户添加")
    @PostMapping("/saveUser")
    public ResUtil<Integer> saveUser(@RequestParam("username") String username, @RequestParam("realname") String realname,
                                     @RequestParam("gender") byte gender, @RequestParam("number") String number,
                                     @RequestParam("roleIds") List<Integer> roleIds) {
        return ResBuilder.ok(userService.saveUser(username, realname, gender, number, roleIds));
    }

    @ApiOperation("用户信息分页展示")
    @GetMapping("/showallUser")
    public ResUtil<String> page(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam("start") int start,
            @RequestParam("limit") int limit) {


        //1 首先要查看一下redis里面是否存在当前内容

        String userPageList = stringRedisTemplate.opsForValue().get("user_page_list");
        if(StringUtils.isBlank(userPageList)){
            //3 没有 ， 先查  存在redis里面
            PageEx<UserDetailVo> pageex = userService.showUserAll(name, start, limit);

            stringRedisTemplate.opsForValue().set("user_page_list", JSON.toJSONString(pageex),3, TimeUnit.SECONDS);
        }

        //2 有不用查询mysql redis返回
        userPageList = stringRedisTemplate.opsForValue().get("user_page_list");
        return ResBuilder.ok(userPageList);
    }

    /**
     * 列出指定的用户id对应的人员信息以及编号信息
     *
     * @param id 编号
     */
    @GetMapping("/oneById")
    @ApiOperation("当前用户个人信息")
    public ResUtil<UserDetailVo> oneById(@RequestParam("id") int id) {
        return ResBuilder.ok(userService.oneById(id));
    }

    /**
     * 列出当前这个id对应的人员所有的角色
     * id userrole
     */
    @GetMapping("/oneAllRoleById")
    @ApiOperation("当前用户所有角色列表")
    public ResUtil<List<Integer>> oneAllRoleById(@RequestParam("id") int id) {
        return ResBuilder.ok(userService.oneAllRoleById(id));
    }

    /**
     * 应该所全部修改要用到的信息一次性给我
     *
     * @return
     */
    @ApiOperation("修改操作")
    @PutMapping("/modifyById")
    public ResUtil<Boolean> modifyUser(@RequestParam List<Integer> roleids, @RequestBody UserDetailVo udv) {
        boolean t = userService.modifyUser(roleids, udv);
        return ResBuilder.ok(t);
    }

    @DeleteMapping("/del/{id}")
    @ApiOperation("根据id进行删除用户操作")
    public ResUtil<Boolean> delById(
            @ApiParam(name = "id", value = "用户主键", required = true) @PathVariable(value = "id") Integer id
    ) {
        return ResBuilder.ok(userService.delById(id));
    }


    @Autowired
    RedisUtilService redisUtilService;

    @GetMapping("/getval")
    public String getValInRedis(@RequestParam("key") String key){
        return redisUtilService.getVal(key);
    }

    @PostMapping("/setval")
    public String setValInRedis(@RequestParam("key") String key,@RequestParam("value") String value){
        redisUtilService.setVal(key,value);
        return "success";
    }
}
